package com.dys.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.List;

/**
 * @className: com.dys.servlet.Servlet01
 * @description: ServletContext上下文对象
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/09/19 22:25
 */
public class Servlet02 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        // 获取web.xml中配置的全局的初始信息
        Enumeration<String> initParameterNames = servletContext.getInitParameterNames();
        while (initParameterNames.hasMoreElements()) {
            String initParameterName = initParameterNames.nextElement();
            String initParameterValue = servletContext.getInitParameter(initParameterName);
            System.out.println(initParameterName + " = " + initParameterValue);
        }

        // 方式2
        System.out.println("*******************************");
        List<String> list = (List<String>) servletContext.getAttribute("list");
        System.out.println(list);
        String sex = (String) servletContext.getAttribute("sex");
        System.out.println("性别：" + sex);
    }
}
